package com.cldz.service;

import com.cldz.common.dto.res.FsuControlResDto;
import com.cldz.dao.FsuControlMapper;
import com.cldz.dao.UserPropertyMapper;
import com.cldz.common.dto.req.FsuControlDto;
import com.cldz.entity.FsuControl;
import com.cldz.utils.BeanCopyUtil;
import com.cldz.utils.MathUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ Author     ：lixiao.
 * @ Date       ：Created in 15:23 2018/12/24
 * @ Description：
 * @ Modified By：
 */
@Service("fsuControlService")
public class FsuControlService {

    @Autowired
    private FsuControlMapper fsuControlMapper;

    @Autowired
    private UserPropertyMapper userPropertyMapper;


    public List<FsuControlResDto> getFsuDevControlRecord(FsuControlDto fsuControlDto){
        List<FsuControl> fsuDevControlRecord = fsuControlMapper.getFsuDevControlRecord(fsuControlDto);
        return copyListBean(fsuDevControlRecord);
    }

    public Integer getFsuDevControlRecordAmount(FsuControlDto fsuControlDto){
        return fsuControlMapper.getFsuDevControlRecordAmount(fsuControlDto);
    }

    public List<FsuControlResDto> getFsuAllControlRecord(FsuControlDto fsuControlDto) {
        setAreaID(fsuControlDto);
        List<FsuControl> fsuAllControlRecord = fsuControlMapper.getFsuAllControlRecord(fsuControlDto);
        return copyListBean(fsuAllControlRecord);

    }

    public Integer getFsuAllControlRecordAmount(FsuControlDto fsuControlDto) {
        setAreaID(fsuControlDto);
        return fsuControlMapper.getFsuAllControlRecordAmount(fsuControlDto);
    }




    private List<FsuControlResDto> copyListBean(List<FsuControl> devListByTypeID) {
        List<FsuControlResDto> list = new ArrayList<>();
        for (FsuControl device : devListByTypeID) {
            FsuControlResDto dto = new FsuControlResDto();
            BeanCopyUtil.copyPropertiesIgnoreNull(device, dto);
//            BeanUtils.copyProperties(device, dto);
            list.add(dto);
        }
        return list;
    }

    private void setAreaID(FsuControlDto deviceDto) {
        Integer areaID = deviceDto.getAreaID();
        if (areaID == null) {
            areaID = userPropertyMapper.queryAreaIDByUserID(Integer.parseInt(deviceDto.getUserID()));
        }
        deviceDto.setAreaID(MathUtils.getAreaIDPrefix(areaID));
    }

}
